﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;
using System.Collections.Specialized;   // string dictionary

/// <summary>
/// Widget for administrator to manage available widgets for apllication users.
/// Enables administrator to :
///  * add new widget into list of available widgets
///  * disable widget 
///  * edit widget's name and description
/// </summary>
public partial class WidgetManagement : WidgetBase
{

    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value>PrototypeWidget</value>
    public override string Name
    {
        get { return "WidgetManagement"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return (string)GetLocalResourceObject("Title"); }
    }


    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
        //this.WidgetType.SelectedIndexChanged += new EventHandler(this.SelectWidgetType);
    }

    /// <summary>
    /// Selects the type of the widget and dispays it's details
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void SelectWidgetType(object sender, EventArgs e)
    {
        this.WidgetTypeDetails.Visible = true;
        WidgetType widgetType = DAOWidgetType.GetWidgetTypeByID(this.WidgetType.SelectedWidgetTypeID);
        DisplayWidgetType(widgetType);
        
    }

    /// <summary>
    /// Displays the type of the widget.
    /// </summary>
    /// <param name="widgetType">Type of the widget.</param>
    private void DisplayWidgetType(WidgetType widgetType)
    {
        if (widgetType != null)
        {
            this.WidgetTypeName.Text = widgetType.Folder;
            this.WidgetTypeDescription.Text = widgetType.Description;
            this.WidgetTypeEnabled.Checked = widgetType.Enabled;
        }
        else
        {
            this.WidgetTypeName.Text = "";
            this.WidgetTypeDescription.Text = "";
            this.WidgetTypeEnabled.Checked = true;
            ShowOperationResult(MessageType.Warning, "WdigetTypeRegisteringHowTo");
        }
        ButtonCreate.Visible = widgetType == null;
        ButtonSave.Visible = widgetType != null;
        WidgetTypeDetails.Visible = true;
        this.WidgetTypeDescription.Visible = widgetType != null;
    }

    /// <summary>
    /// Saves the type of the widget.
    /// </summary>
    private void SaveWidgetType()
    {
        WidgetType widgetType = DAOWidgetType.GetWidgetTypeByID(this.WidgetType.SelectedWidgetTypeID);
        
        widgetType.Name = this.WidgetTypeName.Text;
        widgetType.Description = this.WidgetTypeDescription.Text;
        widgetType.Enabled = this.WidgetTypeEnabled.Checked;

        DAOWidgetType.SaveWidgetType(widgetType);
        this.WidgetTypeDetails.Visible = false;
    }

    /// <summary>
    /// Creates the type of the widget.
    /// </summary>
    /// <param name="widgetType">Type of the widget.</param>
    private void CreateWidgetType()
    {
        DAOWidgetType.CreateWidgetType(
            this.WidgetTypeName.Text,
            this.WidgetTypeDescription.Text,
            this.WidgetTypeEnabled.Checked
        );
        this.WidgetTypeDetails.Visible = false;
    }

    /// <summary>
    /// Handles displaying of form for new widget type
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonCreate_Clicked(object sender, EventArgs e)
    {
        CreateWidgetType();
    }

    /// <summary>
    /// Handles creation of new widget type
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void CreateNewWidgetType(object sender, CommandEventArgs e)
    {
        DisplayWidgetType(null);
    }

    /// <summary>
    /// Saves edited widget type
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonSave_Clicked(object sender, EventArgs e)
    {
        SaveWidgetType();
    }

    /// <summary>
    /// Cancel event handler
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonCancel_Clicked(object sender, EventArgs e)
    {
        this.WidgetTypeDetails.Visible = false;
    }

    #region [ Settings ]

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return false; }
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        this.EditPrototypeWidget.Visible = true;
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        this.EditPrototypeWidget.Visible = false;
    }
    #endregion
    
}
